<?php
class Services extends CI_Controller 
{
	function __construct()
	{
	    //$this->load->helper('functions_helper');
		parent::__construct();
		if((!check_auth()) ? redirect("/login/auth") : site_url("/home"));
		$this->load->model('services_model');
        $this->load->model('sGlobal_model');
	}
	
	/*********************************************************
	**	MODULE SERVICES									    **
	**	AUTHOR 			: ANDI SISWANTO						**
	**	DATE CREATED	: 25/09/2014						**
	*********************************************************/
	function order_equipment()
	{
	    //ambil session
        $CI =& get_instance();
        $data['sesName']=$CI->session->userdata('login')['name'];
        $data['sesJob']=$CI->session->userdata('login')['job'];
        $data['sesNik']=$CI->session->userdata('login')['nik'];
		$data['sesJobPosition']=$CI->session->userdata('login')['jobposition'];
        $data['sesOrg']=$CI->session->userdata('login')['org'];
		//$this->getView($this->uri->segment(1),$this->uri->segment(2),$data);	
        $data['getDetailItem']=$this->sGlobal_model->getDetailEditOrderItem();
		$this->getView(services,add_servOrder,$data);
	}
	function suggestion_and_complaint()
	{
        //ambil session
        $CI =& get_instance();
        $data['sesName']=$CI->session->userdata('login')['name'];
        $data['sesJob']=$CI->session->userdata('login')['job'];
        $data['sesNik']=$CI->session->userdata('login')['nik'];
		$data['sesJobPosition']=$CI->session->userdata('login')['jobposition'];
        $data['sesOrg']=$CI->session->userdata('login')['org'];
		//$this->getView($this->uri->segment(1),$this->uri->segment(2),$data);
        $data['getType']=$this->sGlobal_model->getDetailEditSarKel2();	
        $this->load->view('header');
        $this->load->view('services/add_servSarKel',$data);
		//$this->getView(services,add_servSarKel,$data);
	}
    function paging_info()
	{
	   //ambil session
        $CI =& get_instance();
        $data['sesName']=$CI->session->userdata('login')['name'];
        $data['sesJob']=$CI->session->userdata('login')['job'];
        $data['sesNik']=$CI->session->userdata('login')['nik'];
		$data['sesJobPosition']=$CI->session->userdata('login')['jobposition'];
        $data['sesOrg']=$CI->session->userdata('login')['org'];
        $data['getFloor']=$this->services_model->getServPagFloor();	
        $this->load->view('header');
        $this->load->view('services/add_servPaging',$data);
		//$this->getView(services,add_servSarKel,$data);
	}
	function traffic()
	{
	    //ambil session
        $CI =& get_instance();
        $data['sesName']=$CI->session->userdata('login')['name'];
        $data['sesJob']=$CI->session->userdata('login')['job'];
        $data['sesNik']=$CI->session->userdata('login')['nik'];
		$data['sesJobPosition']=$CI->session->userdata('login')['jobposition'];
        $data['sesOrg']=$CI->session->userdata('login')['org'];
        //$data['getFloor']=$this->services_model->getServPagFloor();	
        $this->load->view('header');
        $this->load->view('services/add_servTraffic',$data);
		//$this->getView(services,add_servSarKel,$data);
	}
    function monitoring(){
        //ambil session
        $CI =& get_instance();
        $data['sesName']=$CI->session->userdata('login')['name'];
        $data['sesJob']=$CI->session->userdata('login')['job'];
        $data['sesNik']=$CI->session->userdata('login')['nik'];
        $data['sesOrg']=$CI->session->userdata('login')['org'];
        //$data['getFloor']=$this->services_model->getServPagFloor();	
        $this->load->view('header');
        $this->load->view('services/add_servMonitoring',$data);
		//$this->getView(services,add_servSarKel,$data);
    }
    function add_servOrderSave(){  
        $this->load->helper('functions_helper');
        //$pCType			    =  $_POST['pCType'];
        //$pOrderId    		=  $_POST['pOrderId'];
        $pHrId   			=  $_POST['pHrId'];
        $pDesc    			=  $_POST['pDesc'];
        $pTglAwal    		=  $_POST['pTglAwal'];
        $pTglAkhir    		=  $_POST['pTglAkhir'];
        $pOrderFor    		=  $_POST['pOrderFor'];
        $pAtasanId    	    =  $_POST['pAtasanId'];
        $pAtasanApp   		=  $_POST['pAtasanApp'];
        $pJumlahItem    	=  $_POST['pJumlahItem'];
        $pItemCek    		=  $_POST['pItemCek'];
        $pItemId    		=  $_POST['pItemId'];
        $pItemName    	    =  $_POST['pItemName'];
        $pItemQty   		=  $_POST['pItemQty'];
        $pOtherId    	    =  $_POST['pOtherId'];
        $pOtherName   		=  $_POST['pOtherName'];
        
        
        //$arr['pCType']			    =  $_POST['pCType'];
        //$arr['pOrderId']    		=  $_POST['pOrderId'];
        $arr['pHrId']   			=  $_POST['pHrId'];
        $arr['pDesc']    			=  $_POST['pDesc'];
        $arr['pTglAwal']    		=  $_POST['pTglAwal'];
        $arr['pTglAkhir']    		=  $_POST['pTglAkhir'];
        $arr['pOrderFor']    		=  $_POST['pOrderFor'];
        $arr['pAtasanId']    	    =  $_POST['pAtasanId'];
        $arr['pAtasanApp']   		=  $_POST['pAtasanApp'];
        $arr['pJumlahItem']    	    =  $_POST['pJumlahItem'];
        $arr['pItemCek']    		=  $_POST['pItemCek'];
        $arr['pItemId']    	    	=  $_POST['pItemId'];
        $arr['pItemName']    	    =  $_POST['pItemName'];
        $arr['pItemQty']       		=  $_POST['pItemQty'];
        $arr['pOtherId']    	    =  $_POST['pOtherId'];
        $arr['pOtherName']   		=  $_POST['pOtherName'];
        
        //var_dump($arr); exit();
        
        $getOrderIdOld=$this->services_model->getOrderIdOld();
        foreach($getOrderIdOld as  $a){
            $cOrderIdOld = $a["ORDER_ID_OLD"];	
    		$cRomawi     = $a["ROMAWI"];	
    		$cOrderIdNew = $cOrderIdOld +1;
        }
    	$getOrderIdNext=$this->services_model->getOrderIdNext($cOrderIdNew,$cRomawi);
        foreach($getOrderIdNext as $a){
            $pOrderID = $a["ORDER_ID"];
        }							
        $arr['pOrderId'] = $pOrderID;
        //var_dump($pOrderID); exit;
        $insertOrderSave=$this->services_model->insertOrderHeader($arr);
        //exit;
    	/*SMS
    		$msg = "FM : Order No $pOrderID telah berhasil.";			
    		
    		// Start Sms
    			
    		// Sms to User
    		$msisdn = dbora_getgamsisdn($pNikFor);
    		send_sms($msisdn, $msg);
    		//End Sms User
    	
    		//End Sms
    	*/
        
        
        for($x=0;$x<$pJumlahItem;$x++){
            $orderSelect1=$this->services_model->orderSelect1(); //ambil nilai terakhir dari order_det_id
            foreach($orderSelect1 as $no){
                $nextNomer=$no['ORDER_DET_ID']+1;
            }
                    
            if($pItemCek[$x]=="Y"){ //Jika tercentang
                if(!empty($pItemQty[$x])){ //jika quantyty tidak kosong
                    $insertOrderItem=$this->services_model->insertOrderItem($pOrderID,$pItemId[$x],$pItemName[$x],$pItemQty[$x],$nextNomer);   
                }  
            }
        }
        
        
        $orderSelect2=$this->services_model->orderSelect1(); //ambil nilai terakhir dari order_det_id
        foreach($orderSelect2 as $no2){
            $nextNomer2=$no2['ORDER_DET_ID']+1;
        }
        
        
        $insertOrderItemOther=$this->services_model->insertOrderItemOther($pOrderID,$pOtherName,$nextNomer2);
        
        $msisdn=$this->sGlobal_model->getMsisdn($pNikFor);
        //sms sini
        send_sms($msisdn, $msg);
    }
    
    function add_servSarKelSave(){
        $this->load->helper('functions_helper'); //Tidak include juga tidak apa2, untuk jaga2 include aja UNTUK PENGGUNAAN SMS
        //$pCType			    =  $_POST['pCType'];
        //$pPoboxId    		=  $_POST['pPoboxId'];
    	$pNik    			=  $_POST['pNik'];
    	$pType    			=  $_POST['pType'];
    	$pCriteria    		=  $_POST['pCriteria'];
    	$pSubject    		=  $_POST['pSubject'];
    	$pDescription  		=  $_POST['pDescription'];
    	$pAtasanNik    		=  $_POST['pAtasanNik'];
    	$pAtasanNama    	=  $_POST['pAtasanNama'];
    	$pNikFor    		=  $_POST['pNikFor'];
    	$pRelatedTo    		=  $_POST['pRelatedTo'];
        
        $arr['pCType']			    =  $_POST['pCType'];
        $arr['pPoboxId']    		=  $_POST['pPoboxId'];
    	$arr['pNik']    			=  $_POST['pNik'];
    	$arr['pType']    			=  $_POST['pType'];
    	$arr['pCriteria']    		=  $_POST['pCriteria'];
    	$arr['pSubject']    		=  $_POST['pSubject'];
    	$arr['pDescription']  		=  $_POST['pDescription'];
    	$arr['pAtasanNik']    		=  $_POST['pAtasanNik'];
    	$arr['pAtasanNama']    	    =  $_POST['pAtasanNama'];
    	$arr['pNikFor']    		    =  $_POST['pNikFor'];
    	$arr['pRelatedTo']    		=  $_POST['pRelatedTo'];
        
        //var_dump($arr); exit;
        
        echo $msisdn; exit;
        $getSarKelIdOld=$this->services_model->getSarKelIdOld();
        foreach($getSarKelIdOld as $a){
            $cPoboxIdOld = $a["POBOX_ID_OLD"];	
			$cRomawi  = $a["ROMAWI"];	
			$cPoboxIdNew = $cPoboxIdOld +1;
        }
        $getSarKelIdNext=$this->services_model->getSarKelIdNext($cPoboxIdNew,$cRomawi);
        foreach($getSarKelIdNext as $a){
            $cPoboxId = $a["POBOX_ID"];
        }
		
        $arr['pPoboxId'] =  $cPoboxId;
        
        $this->services_model->insertSarKel($arr);
        
        
	    $msg = "FM : Order No $cPoboxId telah berhasil, untuk Proses selanjutnya menunggu Approval dari atasan anda $pAtasanNama.";
        $msgatasan = "FM : Order No $cPoboxId membutuhkan approval anda.";
        $msisdn=$this->sGlobal_model->getMsisdn($pNikFor); //SMS KE USER
        //sms sini 
        send_sms($msisdn, $msg);
        $msisdn2=$this->sGlobal_model->getMsisdn($pAtasanNik); //SMS KE ATASAN
        //sms sini
        send_sms($msisdn2, $msgatasan);
        /* OPO IKI
		$stmt2 = "update fm_req_det set req_det_app = 'Y' where req_id='$pReqId' and req_det_id= '$pReqDetId'";
        */
    }
    
    function add_pagingSave(){
        $this->load->helper('functions_helper'); //Tidak include juga tidak apa2, untuk jaga2 include aja UNTUK PENGGUNAAN SMS
        //$pInfoId    	=  $_POST['pInfoId'];
        $pNik    		=  $_POST['pNik'];
        $pSdate    		=  $_POST['pSdate'];
        $pShour    		=  $_POST['pShour'];
        $pSminute    	=  $_POST['pSminute'];
        $pRepetition    =  $_POST['pRepetition'];
        $pRepetitionJumlah=  $_POST['pRepetitionJumlah'];
        $pRepetitionTiap=  $_POST['pRepetitionTiap'];
        $pKeterangan    =  $_POST['pKeterangan'];
        $pSubject    	=  $_POST['pSubject'];
        $pDescription  	=  $_POST['pDescription'];
        $pAtasanNik    	=  $_POST['pAtasanNik'];
        $pAtasanNama    =  $_POST['pAtasanNama'];
        $pNikFor    	=  $_POST['pNikFor'];
        //$pReqId    		=  $_POST['pReqId'];
        //$pReqDetId    	=  $_POST['pReqDetId'];
        $pAccessControlRow  = $_POST['pAccessControlRow'];
        $pFloorCek          =  $_POST['pFloorCek'];
        $pFloorName         =  $_POST['pFloorName'];
        
        //$pInfoId    	=  $_POST['pInfoId'];
        $arr['pNik']    		=  $_POST['pNik'];
        $arr['pSdate']    		=  $_POST['pSdate'];
        $arr['pShour']    		=  $_POST['pShour'];
        $arr['pSminute']    	=  $_POST['pSminute'];
        $arr['pRepetition']    =  $_POST['pRepetition'];
        $arr['pRepetitionJumlah']=  $_POST['pRepetitionJumlah'];
        $arr['pRepetitionTiap']=  $_POST['pRepetitionTiap'];
        $arr['pKeterangan']    =  $_POST['pKeterangan'];
        $arr['pSubject']    	=  $_POST['pSubject'];
        $arr['pDescription']  	=  $_POST['pDescription'];
        $arr['pAtasanNik']    	=  $_POST['pAtasanNik'];
        $arr['pAtasanNama']    =  $_POST['pAtasanNama'];
        $arr['pNikFor']    	=  $_POST['pNikFor'];
        //$pReqId    		=  $_POST['pReqId'];
        //$pReqDetId    	=  $_POST['pReqDetId'];
        $arr['pAccessControlRow']  = $_POST['pAccessControlRow'];
        $arr['pFloorCek']          =  $_POST['pFloorCek'];
        $arr['pFloorName']         =  $_POST['pFloorName'];
        
                
        //var_dump($arr); exit;
        
        $getPagingIdOld=$this->services_model->getPagingIdOld();
        foreach($getPagingIdOld as $a){
            $cInfoIdOld = $a["INFO_ID_OLD"];	
			$cRomawi  = $a["ROMAWI"];	
			$cInfoIdNew = $cInfoIdOld +1;
        }
		//echo $cInfoIdNew;exit;
        $getPagingIdNext=$this->services_model->getPagingIdNext($cInfoIdNew,$cRomawi);
        foreach($getPagingIdNext as $a){
            $cInfoId = $a["INFO_ID"];
        }
        //echo $cInfoId; exit;
        $arr['pInfoId'] = $cInfoId;	
        $this->services_model->insertPaging($arr);


		$msg = "FM : Order No $cInfoId telah berhasil, untuk Proses selanjutnya menunggu Approval dari atasan anda $pAtasanNama.";
		$msgatasan = "FM : Order No $cInfoId membutuhkan approval anda.";
	    $msisdn=$this->sGlobal_model->getMsisdn($pNikFor); //SMS KE USER
        //sms sini 
        send_sms($msisdn, $msg);
        $msisdn2=$this->sGlobal_model->getMsisdn($pAtasanNik); //SMS KE ATASAN
        //sms sini
        send_sms($msisdn2, $msgatasan);
        /*
		$stmt2 = "update fm_req_det set req_det_app = 'Y' where req_id='$pReqId' and req_det_id= '$pReqDetId'";
		$DB3->parse($stmt2);
		$DB3->execute();
		
		$xInfoId = $cInfoId;
        
        */
        for($x=0;$x<$pAccessControlRow;$x++){
            $pagingSelect1=$this->services_model->pagingSelect1(); //ambil nilai terakhir dari INFO_FLOOR_ID
            foreach($pagingSelect1 as $no){
                $nextNomer=$no['INFO_FLOOR_ID']+1;
            }
                    
            if($pFloorCek[$x]=="Y"){ //Jika tercentang
                $this->services_model->insertPagingFloor($nextNomer,$pFloorName[$x],$cInfoId);  
            }
        }	
    }
    function search_traffic(){
        $serial=$_POST["serial"];
        $arr['serial']=$_POST["serial"];
        $data['serial']=$_POST["serial"];
                
        $data['getTraffic']=$this->services_model->getDataTraffic($serial);
        $data['getFloor']=$this->services_model->getDataTrafficFloor();
        $this->load->view('services/add_servTrafficSearch',$data);
    }
    function search_monitoring(){
        $prno=$_POST["prno"];
        $arr['prno']=$_POST["prno"];
        $data['prno']=$_POST["prno"];
                
        $data['getMonitoringCount']=$this->services_model->getDataMonitoringCount($prno);
        $data['getMonitoring']=$this->services_model->getDataMonitoring($prno);
        $this->load->view('services/add_servMonitoringSearch',$data);
    }
    function add_servTrafficSave(){
        $this->load->helper('functions_helper'); //Tidak include juga tidak apa2, untuk jaga2 include aja UNTUK PENGGUNAAN SMS
        $pTrID    	=  $_POST['pTrID'];
    	$pTrUserName 	= $_POST['pTrUserName'];
    	$pTrUserType 	= $_POST['pTrUserType'];
    	$pTrVendorName 	= $_POST['pTrVendorName'];
    	$pTrStartDate 	= $_POST['pTrStartDate'];
    	$pTrEndDate 	= $_POST['pTrEndDate'];
    	$pTrStartHour 	= $_POST['pTrStartHour'];
    	$pTrStartMinute = $_POST['pTrStartMinute'];
    	$pTrEndHour 	= $_POST['pTrEndHour'];
    	$pTrEndMinute 	= $_POST['pTrEndMinute'];
    	$pTrDesc 	= $_POST['pTrDesc'];
    	$pTrFloor	= $_POST['pTrFloor'];
    	$pTrStatus	= $_POST['pTrStatus'];
    	//$pTrAtasanNik	= $_POST['pAtasanNik'];
    	$pTrSerial	= $_POST['pTrSerial'];
    	$pRow  		=  $_POST['pRow'];	
    	$pTrdID		=  $_POST['pTrdID'];
    	$pTrdQty	=  $_POST['pTrdQty'];
    	$pTrdSerial 	=  $_POST['pTrdSerial'];
        
        $arr['pTrID']    	=  $_POST['pTrID'];
    	$arr['pTrUserName'] 	= $_POST['pTrUserName'];
    	$arr['pTrUserType'] 	= $_POST['pTrUserType'];
    	$arr['pTrVendorName'] 	= $_POST['pTrVendorName'];
    	$arr['pTrStartDate'] 	= $_POST['pTrStartDate'];
    	$arr['pTrEndDate'] 	= $_POST['pTrEndDate'];
    	$arr['pTrStartHour'] 	= $_POST['pTrStartHour'];
    	$arr['pTrStartMinute'] = $_POST['pTrStartMinute'];
    	$arr['pTrEndHour'] 	= $_POST['pTrEndHour'];
    	$arr['pTrEndMinute'] 	= $_POST['pTrEndMinute'];
    	$arr['pTrDesc'] 	= $_POST['pTrDesc'];
    	$arr['pTrFloor']	= $_POST['pTrFloor'];
    	$arr['pTrStatus']	= $_POST['pTrStatus'];
    	//$pTrAtasanNik	= $_POST['pAtasanNik'];
    	$arr['pTrSerial']	= $_POST['pTrSerial'];
    	$arr['pRow']  		=  $_POST['pRow'];	
    	$arr['pTrdID']		=  $_POST['pTrdID'];
    	$arr['pTrdQty']	=  $_POST['pTrdQty'];
    	$arr['pTrdSerial'] 	=  $_POST['pTrdSerial'];
        
        //var_dump($arr); exit;
        /*
            // add by Hasim 06-05-2013
            $Traffic_image_name   = (isset($_COOKIE["fm_traffic_imagename"])) ? $_COOKIE["fm_traffic_imagename"] : "";
            $Traffic_image_blob   = "";
            // init file
            $path                   = 'upload/traffic/'.$Traffic_image_name;
    	if(file_exists($path))
            {
                $Traffic_image_blob = file_get_contents($path);
            }
        */    
    	if($pTrStatus == "In") {
    		$pStatus1 = '1';
    	}
    	
    	if(empty($pTrID)) { //JIKA TRID KOSONG, ARTINYA BELUM TERDAFTAR ATO MASUK KERJA 
            $getTrafOldId=$this->services_model->getTrafOldId();
            foreach($getTrafOldId as $a){
                $cNoTrafficIdOld = $a["TRAFFIC_ID_OLD"];	
    			$cRomawi       	 = $a["ROMAWI"];	
    			$cNoTrafficIdNew 	 = $cNoTrafficIdOld +1;
            }
            //echo $cNoTrafficIdOld; exit;
            $getTrafNextId=$this->services_model->getTrafNextId($cNoTrafficIdNew,$cRomawi);
            foreach($getTrafNextId as $a){
                $cNoTrafficID = $a["TRAFFIC_ID"];
            }
            $maxSerialId=$this->services_model->maxSerialId();
            foreach($maxSerialId as $a){
                $maxId=$a['MAXI'];
                $maxIdNext=$maxId+1;
            }
            $this->services_model->insertTraffic($arr,$cNoTrafficID,$maxIdNext);
                    
                    // insert
                    //$DB1->insertBlob($ins1, ':image', $Traffic_image_blob);
                      
    		
    		for($x=0; $x<$pRow; $x++) {			
    			if(empty($pTrdID[$x])) {
    			    $getTrafDetOldId=$this->services_model->getTrafDetOldId();
                    foreach($getTrafDetOldId as $a){
                        $cNoTrafficDetIdOld = $a["TRAFFIC_DET_ID_OLD"];	
    					$cRomawi       	 	= $a["ROMAWI"];	
    					$cNoTrafficDetIdNew = $cNoTrafficDetIdOld +1;
                    }
                    //echo $cNoTrafficDetIdNew; exit;
    				$getTrafDetNextId=$this->services_model->getTrafDetNextId($cNoTrafficDetIdNew,$cRomawi);
                    foreach($getTrafDetNextId as $a){
                        $cNoTrafficDetID = $a["TRAFFIC_DET_ID"];
                    }
                    $this->services_model->insertTrafficDet($cNoTrafficDetID,$pTrdQty[$x],$pTrdSerial[$x],$cNoTrafficID);
    			}
                else {
                    $this->services_model->updateTrafficDet($pTrdQty,$pTrdSerial,$cNoTrafficID,$pTrdID);
    			}
    		}
    	}
        else {
            $this->services_model->updateTraffic($pTrEndHour,$pTrEndMinute,$pTrID);
    	}
    	
            
            /// delete temporary photo
            /*while(is_file($path) == TRUE)
            {
                chmod($path, 0666);
                unlink($path);  // delete file
            }*/
    }
	/*=======================================================
	 Function 		: Load static header and footer view page 
	 				  and dinamic page view page from each 
					  function 
	 AUTHOR			: ANDI SISWANTO
	 CREATED DATE	: 25/09/2014
	=======================================================*/	
	function getView($P_Menu,$S_Menu, $data)
	{
		$this->load->view('header');
		if ($data)
		{
		//	var_dump($data);
			$this->load->view($P_Menu.'/'.$S_Menu,$data);
		}
		else
		{
		//	var_dump($data);
			$this->load->view($P_Menu.'/'.$S_Menu);
		}
		$this->load->view('footer');
	}
    
    
    function globals_nik_SearchGlobal()
	{
		$Emp_Name[] = SearchDetailEmp($this->security->xss_clean($this->input->post('SearchName')));
		foreach($Emp_Name AS $Emp_Name =>$keyVal)
		{
			$data['Show_Name'] = $keyVal['arr_Name'];
		}
		echo json_encode($data);
	}
	
	function globals_nik_detail()
	{
		$data['DetailEmp'] = putDetailEmp($this->security->xss_clean($this->input->post('id')));
		echo json_encode($data);
	}
    
    function globals_rel_detail()
	{
		$data['DetailRel'] = putDetailRel($this->security->xss_clean($this->input->post('id')));
		echo json_encode($data);
	}
    function globals_rel_SearchGlobal()
	{
		$Rel_Name[] = SearchDetailRel($this->security->xss_clean($this->input->post('SearchName')));
		foreach($Rel_Name AS $Rel_Name =>$keyVal)
		{
			$data['Show_Name'] = $keyVal['arr_Name'];
		}
		echo json_encode($data);
	}
    function globals_serial_SearchGlobal()
	{
		$Ser_Name[] = SearchDetailSer($this->security->xss_clean($this->input->post('SearchName')));
		foreach($Ser_Name AS $Ser_Name =>$keyVal)
		{
			$data['Show_Name'] = $keyVal['arr_Name'];
		}
		echo json_encode($data);
	}
    function globals_monitoring_SearchGlobal()
	{
		$Mon_Name[] = SearchDetailMon($this->security->xss_clean($this->input->post('SearchName')));
		foreach($Mon_Name AS $Mon_Name =>$keyVal)
		{
			$data['Show_Name'] = $keyVal['arr_Name'];
		}
		echo json_encode($data);
	}
}
?>